package ar.edu.unq.tpi.uis.tepe3.wicket.panels;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractListPage;

public class DefaultListViewActionsPanel extends Panel {

	public DefaultListViewActionsPanel(String id, IModel model) {
		super(id, model);
		
		add(new Link("edit", model) {
		
			@Override	
			public void onClick() {
				String className = "ar.edu.unq.tpi.uis.tepe3.wicket.pages." + getModelObject().getClass().getSimpleName() + "FormPage";
				
				Class<?> targetClass;
				try {
					targetClass = Class.forName(className);
					Constructor<?> constructor = targetClass.getConstructor(IModel.class);
					Page aPage = (Page) constructor.newInstance(getModel());
					this.setResponsePage(aPage);
				} catch (ClassNotFoundException e) {
					warn("Ups!:" + e.getMessage());
				} catch (SecurityException e) {
					warn("Ups!:" + e.getMessage());
				} catch (NoSuchMethodException e) {
					warn("Ups!:" + e.getMessage());
				} catch (IllegalArgumentException e) {
					warn("Ups!:" + e.getMessage());
				} catch (InstantiationException e) {
					warn("Ups!:" + e.getMessage());
				} catch (IllegalAccessException e) {
					warn("Ups!:" + e.getMessage());
				} catch (InvocationTargetException e) {
					warn("Ups!:" + e.getMessage());
				}
				
//				
//				Class aPage = null;
//				this.setResponsePage(aPage);
			}
		});
		
		add(new Link("delete", model) {
			@Override	
			public void onClick() {
				((AbstractListPage) getPage()).deleteAction(getModel());
			}
		});
	}

}
